package com.sooncode.project.focusservice.controller;

import com.alibaba.fastjson.JSON;
import com.sooncode.project.core.finder.Page;
import com.sooncode.project.core.model.DomainException;
import com.sooncode.project.focusservice._.base.Utils;
import com.sooncode.project.focusservice.model.*;
import com.sooncode.project.focusservice.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("device")
public class DeviceController {
    @Autowired
    OrganizationService orgService;
    @Autowired
    TeacherService teacherService;
    @Autowired
    MemberService memberService;
    @Autowired
    TestService testService;
    @Autowired
    CourseSettingService courseService;
    @Autowired
    FeeService feeService;
    @Autowired
    CourseHourService courseHourService;
    @Autowired
    DownloadCourseService downloadCourseService;
    @Autowired
    SensoryService sensoryService;


    @PostMapping("publicApi/login")
    public Object login(@RequestParam Map params) {
        Map map=new HashMap();
        try {
            map=orgService.login(params);
            map.put("code","0000");
            map.put("desc","success");
        }catch (Exception e){
            map.put("code","0001");
            map.put("desc",e.getMessage());
        }
        return map;
    }
    @PostMapping("getMemberList")
    public Object getMemberList(@RequestParam Map<String,Object> params) {
        Map<String,Object> result=new HashMap();
        try {
            Utils.mapValueTrim(params);
            Object obj = memberService.getMemberList(params);
            result.put("code","0000");
            result.put("desc","success");
            result.put("data",obj);

        }catch (Exception ex){
            result.put("code","0001");
            result.put("desc",ex.getMessage());
        }
        return result;
    }
    @PostMapping("postMemberTest")
    public Object postMemberTest(@RequestBody Map params) {
        System.out.println(JSON.toJSON(params));
        Map result=new HashMap();
        try {
            Object score=testService.addTest(params);
            result.put("code","0000");
            result.put("desc","success");
            result.put("data",score);
        }catch (Exception ex){
            ex.printStackTrace();
            result.put("code","0001");
            result.put("desc",ex.getMessage());
        }
        return result;
    }
    @GetMapping("searchMember")
    public List<Member> searchMember(@RequestParam Map<String,Object> map){
        return memberService.getMemberList(map);
    }
    @GetMapping("currOrg")
    public Organization getCurOrg(){
        return orgService.getCurOrg();
    }
    //设备端评测老师接口
    @PostMapping("teacher")
    public boolean addTeacher(@RequestParam Map map){
        return teacherService.add(map);
    }
    @PutMapping("teacher")
    public boolean modifyTeacher(@RequestParam String id,@RequestParam Map map){
        return teacherService.modify(id,map);
    }
    @DeleteMapping("teacher")
    public boolean deleteTeacher(@RequestParam String[] ids){
        for(String id:ids){
            teacherService.delete(id);
        }
        return true;
    }
    @GetMapping("teachers")
    public Page<Teacher> getTeacherPageList(@RequestParam String orgId,@RequestParam int pageSize, @RequestParam int pageIndex, @RequestParam Map map){
        map.remove("pageSize");
        map.remove("pageIndex");
        map.put("orgId",orgId);
        Utils.mapValueTrim(map);
        return teacherService.getPageList(pageSize,pageIndex-1,map);
    }
    @GetMapping("teacherList")
    public List<Teacher> getTeacherList(@RequestParam String orgId){
        if(StringUtils.isEmpty(orgId))
            throw new DomainException("orgId不能为空");
        Map map=new HashMap();
        map.put("orgId",orgId);
        return teacherService.getList(map);
    }
    //设备端会员档案接口
    @PostMapping("member")
    public boolean add(@RequestParam Map map){
        return memberService.addForDevice(map);
    }
    @PutMapping("member")
    public boolean modify(@RequestParam String id,@RequestParam Map map){
        return memberService.modifyForDevice(id,map);
    }
    @DeleteMapping("member")
    public boolean delete(@RequestParam String[] ids){
        for(String id:ids){
            memberService.delete(id);
        }
        return true;
    }
    @GetMapping("members")
    public Page<Member> getMemberPageList(@RequestParam int pageSize, @RequestParam int pageIndex, @RequestParam Map map){
        map.remove("pageSize");
        map.remove("pageIndex");
        Utils.mapValueTrim(map);
        return memberService.getPageListForDevice(pageSize,pageIndex-1,map);
    }
    @PostMapping("importMembers")
    public boolean importMembers(@RequestParam String text){
        return memberService.importMembersForDevice(text);
    }
    //设备端费用统计
    @GetMapping("fees")
    public Page<Fee> getFeePageList(@RequestParam int pageSize, @RequestParam int pageIndex, @RequestParam Map map){
        map.remove("pageSize");
        map.remove("pageIndex");
        Utils.mapValueTrim(map);
        return feeService.getPageListForDevice(pageSize,pageIndex-1,map);
    }
    @PostMapping("fee")
    public boolean addFee(@RequestParam Map map){
        return feeService.addForDevice(map);
    }
    @PutMapping("fee")
    public boolean modifyFee(@RequestParam String id,@RequestParam Map map){
        return feeService.modifyForDevice(id,map);
    }
    @DeleteMapping("fee")
    public boolean deleteFee(@RequestParam String[] ids){
        for(String id:ids){
            feeService.delete(id);
        }
        return true;
    }
    //设备端评测列表
    @GetMapping("tests")
    public Page<Test> getTestPageList(@RequestParam int pageSize, @RequestParam int pageIndex, @RequestParam Map map){
        map.remove("pageSize");
        map.remove("pageIndex");
        Utils.mapValueTrim(map);
        return testService.getPageListForDevice(pageSize,pageIndex-1,map);
    }
    //设备端课程设置
    @GetMapping("courseSettings")
    public Page<CourseSetting> getCourseSettingPageList(@RequestParam int pageSize, @RequestParam int pageIndex, @RequestParam Map map){
        map.remove("pageSize");
        map.remove("pageIndex");
        Utils.mapValueTrim(map);
        return courseService.getPageListForDevice(pageSize,pageIndex-1,map);
    }
    //导出课程包
    @GetMapping("publicApi/exportCourse")
    public void exportCourse(@RequestParam String testId,HttpServletResponse response){
        courseService.exportCourse(testId,response);
    }
    @GetMapping("publicApi/downloadCourse")
    public void downloadCourse(
            @RequestParam String testId,
            @RequestParam String reTestId,
            @RequestParam String phase,
            HttpServletResponse response){
        downloadCourseService.downloadCourse(testId,reTestId,phase,response);
    }
    //课时管理
    @PostMapping("courseHour")
    public boolean addCourseHour(@RequestParam Map map){
        return courseHourService.add(map);
    }
    @PutMapping("courseHour")
    public boolean modifyCourseHour(@RequestParam String id,@RequestParam Map map){
        return courseHourService.modify(id,map);
    }
    @DeleteMapping("courseHour")
    public boolean deleteCourseHour(@RequestParam String[] ids){
        for(String id:ids){
            courseHourService.delete(id);
        }
        return true;
    }
    @GetMapping("courseHours")
    public Page<CourseHour> getCourseHourPageList(@RequestParam String memberId,@RequestParam int pageSize, @RequestParam int pageIndex, @RequestParam Map map){
        map.remove("pageSize");
        map.remove("pageIndex");
        map.remove("memberId");
        Utils.mapValueTrim(map);
        return courseHourService.getPage(memberId,pageSize,pageIndex-1,map);
    }
    @PutMapping("generatePadCode")
    public boolean generatePadCode(@RequestParam String id){
        return memberService.generatePadCode(id);
    }
    @PutMapping("customInfo")
    public boolean modifyCustomInfo(@RequestParam Map map){
        return orgService.modifyCustomInfo(map);
    }

    @PostMapping("sensory")
    public boolean addSensory(@RequestParam Map map){
        return sensoryService.add(map);
    }
    @PutMapping("sensory")
    public boolean modifySensory(@RequestParam String id,@RequestParam Map map){
        return sensoryService.modify(id,map);
    }
    @DeleteMapping("sensory")
    public boolean deleteSensory(@RequestParam String[] ids){
        for(String id:ids){
            sensoryService.delete(id);
        }
        return true;
    }
    @GetMapping("sensorys")
    public Page<Sensory> getSensoryPageList(@RequestParam int pageSize, @RequestParam int pageIndex, @RequestParam Map map){
        map.remove("pageSize");
        map.remove("pageIndex");
        Utils.mapValueTrim(map);
        return sensoryService.getDevicePageList(pageSize,pageIndex-1,map);
    }
    @GetMapping("sensoryById")
    public Sensory getSensoryById(@RequestParam String id){
        return sensoryService.getById(id);
    }

    @GetMapping("publicApi/sensoryById")
    public Sensory getSensoryByIdForDevice(@RequestParam String id){
        return sensoryService.getById(id);
    }

}
